import 'package:flutter/material.dart';
import 'package:usb_serial/usb_serial.dart';
import 'package:usbtest/SerialPage.dart';
void main() {
  runApp(const MyApp());
}
class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: '串口测试'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;
  @override
  State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  List<UsbDevice> devices=[];
  @override
  void initState() {
    super.initState();
    UsbSerial.usbEventStream?.listen((e) async {
    devices=await UsbSerial.listDevices();
    setState(() {});
    });
    Future.delayed(Duration.zero).then((value) async {
      devices=await UsbSerial.listDevices();
    setState(() {});
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
      // Row(mainAxisAlignment: MainAxisAlignment.spaceAround,children: [
      //   ElevatedButton(onPressed:() async {
      //     var vid=6790;
      //     var pid=29987;
      //     var did=2006;
      //     var p = await UsbSerial.create(vid,pid);
      //     // var p = await UsbSerial.createFromDeviceId(did);
      //     await p?.open();
      //     p?.write(Uint8List.fromList([0xA0,0x01,0x01,0xA2]));
      //   }, child:const Text("打开指定设备")),
      //   ElevatedButton(onPressed:()async{
      //     var vid=6790;
      //     var pid=29987;
      //     var did=2003;
      //     var p = await UsbSerial.create(vid,pid);
      //     // var p = await UsbSerial.createFromDeviceId(did);
      //     await p?.open();
      //     p?.write(Uint8List.fromList([0xA0,0x01,0x00,0xA1]));
      //     await p?.close();
      //   }, child:const Text("关闭指定设备")),
      //   ElevatedButton(onPressed:(){
      //     Navigator.push(context, MaterialPageRoute(builder:(c)=>IMOpenSerial()));
      //   }, child:const Text("Im"))
      // ],),
            Expanded(child:(devices.isNotEmpty)? ListView.builder(itemBuilder:(c,i)=> InkWell(
              onTap:()async{
                Navigator.push(context, MaterialPageRoute(builder:(c)=>SerialPage(usbdevices: devices[i],)));
              },child:Container(
                padding: const EdgeInsets.symmetric(vertical: 10,horizontal: 10),
                decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: const BorderRadius.all(Radius.circular(5)),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.grey.withAlpha(50),
                      offset: const Offset(1, 1),
                      spreadRadius: 2,
                      blurRadius: 5
                    )
                  ]
                ),
                child: Column(crossAxisAlignment: CrossAxisAlignment.start,children:[
                  Text("设备Id：${devices[i].deviceId}"),
                  Text("pid：${devices[i].pid}"),
                  Text("vid：${devices[i].vid}"),
                  Text("设备名字(路径)：${devices[i].deviceName}"),
                  Text("产品名字：${devices[i].productName}"),
                ],),
              ),
            ),
              itemCount: devices.length,):const Text("请插入USB设备"))
          ],
        ),
      ),
    );
  }
}